package libtool65.link;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import libtool65.o65.ExportDefinition;
import libtool65.o65.ExportDefinitions;
import libtool65.o65.References;

public class SymbolTable
{
	private HashMap<String, Symbol> symbols = new HashMap<String, Symbol>(); 
	private HashSet<String> unresolved = new HashSet<String>();
	
	public void addModule(ModuleDefinition module, References references, ExportDefinitions exports)
	{
		for (String reference : references.toArray())
		{
			if (!symbols.containsKey(reference))
				unresolved.add(reference);
		}
		for (ExportDefinition export : exports.toArray())
		{
			if (symbols.containsKey(export.getName()))
			{
				System.out.println("warning: symbol "+export.getName()+" in "+module.getName()
						+" already defined in "+symbols.get(export.getName()).getModuleDefinition().getName());
			}
			else
			{
				symbols.put(export.getName(), new Symbol(export.getName(), module));
			}
			unresolved.remove(export.getName());
		}
	}

	public boolean hasUnresolvedReferences() {
		return !unresolved.isEmpty();
	}

	public List<String> getUnresolvedReferences() {
		return new ArrayList<String>(unresolved);
	}

	public Symbol resolve(String label) {
		return symbols.get(label);
	}
}
